package com.bestcem.xm.ticket.controller.v2;

import cn.hutool.core.lang.ObjectId;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.common.core.enums.ReqErrorCodeEnum;
import com.bestcem.xm.component.security.context.SecurityContextHolder;
import com.bestcem.xm.ticket.constant.Constants;
import com.bestcem.xm.ticket.controller.param.ticket.config.CreateConfigParam;
import com.bestcem.xm.ticket.controller.param.ticket.config.UpdateConfigParam;
import com.bestcem.xm.ticket.controller.vo.SimpleIdVO;
import com.bestcem.xm.ticket.controller.vo.ticket.config.TicketConfigInfoVO;
import com.bestcem.xm.ticket.service.TicketConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

/**
 * 单个工单配置的增删改查
 *
 * @author liheng
 * @version v1.0
 * @date 2021/8/18 13:54
 */
@Api(tags = "单个工单配置的增删改查")
@RestController(value = "TicketV2TicketConfigController")
@RequestMapping(value = "ticket/v2/ticketconfigs", produces = {MediaType.APPLICATION_JSON_VALUE})
@Slf4j
public class TicketConfigController {

    @Resource
    private HttpServletRequest request;

    @Resource
    private TicketConfigService ticketConfigService;

    /**
     * 查询工单配置信息
     *
     * @param id 工单配置id
     * @return json
     */
    @ApiOperation(value = "ticketConfigs 简单查询(id)")
    @GetMapping(value = "/{id}")
    public XmResultJson<TicketConfigInfoVO> getTicketConfig(@PathVariable String id) {
        if (!ObjectId.isValid(id)) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_FORMAT, "id错误");
        }
        ServiceResult<TicketConfigInfoVO> ticketConfig = ticketConfigService.getTicketConfig(id);
        if (ticketConfig.isSuccess()) {
            return XmResultJson.success(ticketConfig.getData());
        }
        return XmResultJson.fail(ReqErrorCodeEnum.PARAM_FORMAT, ticketConfig.getMsg());

    }


    @ApiOperation(value = "ticketConfigs 简单新增(param)")
    @PostMapping(value = "/")
    public XmResultJson<SimpleIdVO> saveTicketConfig(@RequestBody CreateConfigParam param) {
        String userId = SecurityContextHolder.getUserId();
        
        String orgId = SecurityContextHolder.getOrgId();
        param.setUserId(userId);
        param.setOrgId(orgId);
        param.setSurveyId(Optional.ofNullable(param.getSurveyId()).orElse(Constants.BLANK));

        ServiceResult<String> result = ticketConfigService.insertTicketConfig(param);
        if (!result.isSuccess()) {
            return XmResultJson.fail(result.getErrorData(), result.getMsg());
        }
        return XmResultJson.success(new SimpleIdVO(result.getData()));
    }


    @ApiOperation(value = "ticketConfigs 简单更新(id, param)")
    @PutMapping(value = "/{id}")
    public XmResultJson<SimpleIdVO> updateTicketConfig(@PathVariable(value = "id") String id, @RequestBody UpdateConfigParam param) {
        if (!ObjectId.isValid(id)) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_FORMAT, "id格式错误");
        }
        // 从token中获取orgId，userId
        String userId = SecurityContextHolder.getUserId();
        
        String orgId = SecurityContextHolder.getOrgId();
        param.setUserId(userId);
        param.setOrgId(orgId);
        log.error("[Ticket] updateTicketConfig={}", param);
        ServiceResult<String> result = ticketConfigService.updateTicketConfigV2(id, param);
        if (!result.isSuccess()) {
            return XmResultJson.fail(result.getErrorData(), result.getErrorData().getMsg());
        }
        return XmResultJson.success(new SimpleIdVO(result.getData()));
    }
}
